package ch.unibe.iam.scg.elexis_statistics.model;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

import ch.elexis.util.Money;


/**
 * A ViewerSorter which can sort top down and bottom up depending on the setting
 * of the reverse boolean.
 * 
 * $Id: ColumnSorter.java 642 2008-12-08 10:24:26Z psiska $
 * 
 * @author Peter Siska
 * @author Dennis Schenk
 * @version $Rev: 642 $
 */
public class ColumnSorter extends ViewerSorter {

	/** Reverse ordering */
	private boolean reverse = false;

	/** Index of the column which should be used to sort the results. */
	private int index;

	public ColumnSorter(int index) {
		this.index = index;
	}

	// TODO number sorting. meta data?
	@Override
	public int compare(Viewer viewer, Object e1, Object e2) {
		Object o1 = ((Object[]) e1)[this.index];
		Object o2 = ((Object[]) e2)[this.index];

		int result;
		if (o1.getClass() == Money.class && o2.getClass() == Money.class) {
			result = ((Money) o1).compareTo((Money) o2);

		} else if (o1.getClass().getSuperclass() == Number.class && o2.getClass().getSuperclass() == Number.class) {
			Double d1 = ((Number) o1).doubleValue();
			Double d2 = ((Number) o2).doubleValue();
			System.out.println(o1.getClass());
			result = d1.compareTo(d2);

		} else {
			result = o1.toString().compareTo(o2.toString());
		}

		return (this.reverse ? result * (-1) : result); // invert result if
														// reverse
	}

	/** reverse ordering */
	public void setReverse(boolean reverse) {
		this.reverse = reverse;
	}

	public int getIndex() {
		return this.index;
	}

	public void setIndex(int index) {
		this.index = index;
	}
}
